<?php

/**
 * 登录控制器.
 * User: Administrator
 * Date: 2017/10/18 0018
 * Time: 10:00
 */

namespace User\Controller;

use Think\Controller;

class UserController extends Controller
{
    /**
     * 登录
     */
    public function post_login()
    {
        if (IS_PSOT) {
            $phone = I('post.phone');
            $password = I('post.password');
            $j_push_id = I('post.j_push_id');
            /*if (empty($j_push_id)) {
                $this->ajaxReturn(['status' => 0, 'msg' => '请上传推送id！']);
            }*/
            $userLogin = D('UserLogin');
            $result = $userLogin->Login($phone, $password);
            if ($result['status'] != 1) {
                $this->ajaxReturn($result);
            }
            // 更新推送id
            M('User')->where(['user_id' => $result['msg']['user_id']])->setField('j_push_id', $j_push_id);
            $this->ajaxReturn($result);
            
        } else {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }
    /**
     * 企业类别列表
     */
    public function get_type()
    {
        $typeM = M('type_list');
        $result = $typeM->select();
        if (empty($result)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
        $data['status']=1;
        $data['msg']=$result;
        $this->ajaxReturn($data);
       
    }
    /**
     * 行业类别列表
     */
    public function industry_list()
    {
        $typeM = M('hy_type');
        $result = $typeM->select();
        if (empty($result)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
        $data['status']=1;
        $data['msg']=$result;
        $this->ajaxReturn($data);
       
    }
    /**
     * 支持审批项目类别
     */
    public function get_approval_type(){
        $result = M('approval_type')->select();
        if(empty($result)){
            $this->ajaxReturn(['status'=>0,'msg'=>'参数错误！']);
        }
        $data['status'] = 1;
        $data['msg'] = $result;
        $this->ajaxReturn($data);

    }
    /**
     * 企业申请修改安全员
     */
    public function apply_update_safety(){
        $data['user_id'] = I('post.user_id');
        $data['company_id'] = I('post.company_id');
        if(empty($data['company_id'])){
            $this->ajaxReturn(['status' => 0, 'msg' => '缺少company_id']);
        }
        $data['safety_officer'] = I('post.safety_officer');
        $data['safety_officer_phone'] = I('post.safety_officer_phone');
        $data['safety_officer_photo'] = I('post.safety_officer_photo');
        $data['create_time'] = time();
        $result = M('update_safety')->add($data);
        $apply['item_id'] = $result;
        $apply['handle_type'] = '1';
        $apply['push_user_id'] = $data['user_id'];
        $apply['create_time'] = time();
        $res = M('handle_list')->add($apply);
        $user = M('user')->where(['user_id'=>$data['user_id']])->find();
        $mobile = $user['phone']; 
        $this->http_send_post($mobile,'【临沭安监】你的申请修改企业安全员已经提交，请等待审批！');
        $user_id = $data['user_id'];
        $j_push_id = M('User')->where(['user_id' => $user_id])->getField('j_push_id');
        if (!empty($j_push_id)) {
            jPushs('你的申请修改企业安全员已经提交，请等待审批！', 0, $j_push_id, $user_id);
        }
        if($result){
            $this->ajaxReturn(['status' => 1, 'msg' => $data]);
        }else{
            $this->ajaxReturn(['status' => 0, 'msg' => '添加失败']);
        }
    }
    /**
     * 企业注册
     */
    public function post_register()
    {
        if (IS_PSOT) {
            $postdata = I('post.');
            // if (empty($j_push_id)) {
            //     $this->ajaxReturn(['status' => 0, 'msg' => '请上传推送id！']);
            // }
            $companyData['name'] = $postdata['name'];
            $companyData['code'] = $postdata['code'];
            $companyData['capital'] = $postdata['capital'];
            $companyData['address'] = $postdata['address'];
            $companyData['type'] = $postdata['type'];
            $companyData['gps_position'] = $postdata['gps_position'];
            $position = explode(',', $companyData['gps_position']);
            $companyData['lng'] = $position[0];
            $companyData['lat'] = $position[1];
            $companyData['person_liable'] = $postdata['person_liable'];
            $companyData['liable_phone'] = $postdata['liable_phone'];
            $companyData['safety_officer'] = $postdata['safety_officer'];
            $companyData['safety_officer_phone'] = $postdata['safety_officer_phone'];
            $companyData['safety_officer_photo'] = $postdata['safety_officer_photo'];
            $companyData['account'] = $postdata['account'];
            $companyData['password'] = md5($postdata['password']);
            $companyData['danger_a'] = $postdata['danger_a'];
            $companyData['danger_b'] = $postdata['danger_b'];
            $companyData['danger_c'] = $postdata['danger_c'];
            $companyData['j_push_id'] = $postdata['j_push_id'];
            $companyData['create_time'] = time();
            $companyData['other'] = $postdata['other'];
            $companyM = M('Company');
            $resultCompany = $companyM->add($companyData);
            $com = $companyM->where(['name'=>$companyData['name']])->find();
            if($companyData['name'] == $com['name']){
                $this->ajaxReturn(['status'=>0,'msg'=>'该企业已经注册过，请不要重复注册！']);
                exit;
            }
            $userData['username'] = $companyData['person_liable'];
            $userData['phone'] = $companyData['account'];
            $userData['password'] = $companyData['password'];
            $userData['j_push_id'] = $companyData['j_push_id'];
            $userData['level_id'] = 5;
            $userData['role_id'] = 2;
            $userData['company_id'] = $resultCompany;

            $returns['company'] = $companyData;
            $returns['user'] = $userData;
            
            $userM = M('User');
            $resultUser = $userM->add($userData);

            $this->ajaxReturn(['status' => 1, 'msg' => $returns]);
            exit;
        } else {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }

    /**
     * 新企业注册
     */
    public function post_register2()
    {
        if (IS_PSOT) {
            $postdata = I('post.');
            // if (empty($j_push_id)) {
            //     $this->ajaxReturn(['status' => 0, 'msg' => '请上传推送id！']);
            // }
            $companyData['name'] = $postdata['name'];
            $companyData['code'] = $postdata['code'];
            $companyData['capital'] = $postdata['capital'];
            $companyData['address'] = $postdata['address'];
            $companyData['type'] = $postdata['type'];
            $companyData['gps_position'] = $postdata['gps_position'];
            $position = explode(',', $companyData['gps_position']);
            $companyData['lng'] = $position[0];
            $companyData['lat'] = $position[1];
            $companyData['person_liable'] = $postdata['person_liable'];
            $companyData['liable_phone'] = $postdata['liable_phone'];
            $companyData['safety_officer'] = $postdata['safety_officer'];
            $companyData['safety_officer_phone'] = $postdata['safety_officer_phone'];
            $companyData['safety_officer_photo'] = $postdata['safety_officer_photo'];
            $companyData['account'] = $postdata['account'];
            $companyData['password'] = md5($postdata['password']);
            /*$companyData['danger_a'] = $postdata['danger_a'];
            $companyData['danger_b'] = $postdata['danger_b'];
            $companyData['danger_c'] = $postdata['danger_c'];
            $companyData['other'] = $postdata['other'];
            */
            $companyData['industry_type'] = $postdata['industry_type'];
            $companyData['scale'] = $postdata['scale'];
            $companyData['danger_name'] = $postdata['danger_name'];
            $companyData['annual_use'] = $postdata['annual_use'];
            $companyData['stock'] = $postdata['stock'];
            $companyData['device_name'] = $postdata['device_name'];
            $companyData['raw_material'] = $postdata['raw_material'];
            $companyData['desc_process'] = $postdata['desc_process'];
            $companyData['safe_is_create'] = $postdata['safe_is_create'];
            $companyData['safe_level'] = $postdata['safe_level'];
            $companyData['standard_id'] = $postdata['standard_id'];
            $companyData['certificate'] = $postdata['certificate'];
            $companyData['prevent_is_create'] = $postdata['prevent_is_create'];
            $companyData['through_evaluation'] = $postdata['through_evaluation'];
            $companyData['review_unit'] = $postdata['review_unit'];
            $companyData['is_run'] = $postdata['is_run'];
            $companyData['evaluation_report'] = $postdata['evaluation_report'];
            $companyData['review_record'] = $postdata['review_record'];
            $companyData['is_plan'] = $postdata['is_plan'];
            $companyData['record_id'] = $postdata['record_id'];
            $companyData['record_img'] = $postdata['record_img'];
            $companyData['goods_name'] = $postdata['goods_name'];
            $companyData['specification_model'] = $postdata['specification_model'];
            $companyData['total'] = $postdata['total'];
            $companyData['unit'] = $postdata['unit'];
            $companyData['person_charge'] = $postdata['person_charge'];
            $companyData['safety_precautions'] = $postdata['safety_precautions'];
            $companyData['safe_record'] = $postdata['safe_record'];
            $companyData['record_prove_img'] = $postdata['record_prove_img'];
            $companyData['third_party'] = $postdata['third_party'];
            $companyData['organization_name'] = $postdata['organization_name'];
            $companyData['linkman'] = $postdata['linkman'];
            $companyData['phone'] = $postdata['phone'];
            $companyData['descript'] = $postdata['descript'];
            $companyData['j_push_id'] = $postdata['j_push_id'];

            $companyData['create_time'] = time();
            $companyM = M('Company');
            $com = $companyM->where(['name'=>$companyData['name']])->find();
            if($companyData['name'] == $com['name']){
                $this->ajaxReturn(['status'=>0,'msg'=>'该企业已经注册过，请不要重复注册！']);
                exit;
            }
            $resultCompany = $companyM->add($companyData);
            
            $userData['username'] = $companyData['person_liable'];
            $userData['phone'] = $companyData['account'];
            $userData['password'] = $companyData['password'];
            $userData['j_push_id'] = $companyData['j_push_id'];
            $userData['level_id'] = 5;
            $userData['role_id'] = 2;
            $userData['company_id'] = $resultCompany;

            $returns['company'] = $companyData;
            $returns['user'] = $userData;
            
            $userM = M('User');
            $resultUser = $userM->add($userData);

            $this->ajaxReturn(['status' => 1, 'msg' => $returns]);
            exit;
        } else {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }
    /**
     * 企业修改2
     */
    public function update_info2()
    {
        if (IS_PSOT) {
            $postdata = I('post.');
            /*if (empty($j_push_id)) {
                $this->ajaxReturn(['status' => 0, 'msg' => '请上传推送id！']);
            }*/
            $whereUser['user_id'] = $postdata['user_id'];
            $company_id = $postdata['company_id'];
            if($whereUser['user_id']<=0){
                $this->ajaxReturn(['status' => 0, 'msg' => '缺少user_id']);
            }
            if(!empty($company_id)){
                $whereUser['company_id'] = $company_id;
            }
            $companyData['name'] = $postdata['name'];
            $companyData['code'] = $postdata['code'];
            $companyData['capital'] = $postdata['capital'];
            $companyData['address'] = $postdata['address'];
            $companyData['type'] = $postdata['type'];
            $companyData['gps_position'] = $postdata['gps_position'];
            $companyData['person_liable'] = $postdata['person_liable'];
            $companyData['liable_phone'] = $postdata['liable_phone'];
            $companyData['safety_officer'] = $postdata['safety_officer'];
            $companyData['safety_officer_phone'] = $postdata['safety_officer_phone'];
            $companyData['safety_officer_photo'] = $postdata['safety_officer_photo'];
            //$companyData['account'] = $postdata['account'];
           // $companyData['password'] = md5($postdata['password']);
            
            $companyData['industry_type'] = $postdata['industry_type'];
            $companyData['scale'] = $postdata['scale'];
            $companyData['danger_name'] = $postdata['danger_name'];
            $companyData['annual_use'] = $postdata['annual_use'];
            $companyData['stock'] = $postdata['stock'];
            $companyData['device_name'] = $postdata['device_name'];
            $companyData['raw_material'] = $postdata['raw_material'];
            $companyData['desc_process'] = $postdata['desc_process'];
            $companyData['safe_is_create'] = $postdata['safe_is_create'];
            $companyData['safe_level'] = $postdata['safe_level'];
            $companyData['standard_id'] = $postdata['standard_id'];
            $companyData['certificate'] = $postdata['certificate'];
            $companyData['prevent_is_create'] = $postdata['prevent_is_create'];
            $companyData['through_evaluation'] = $postdata['through_evaluation'];
            $companyData['review_unit'] = $postdata['review_unit'];
            $companyData['is_run'] = $postdata['is_run'];
            $companyData['evaluation_report'] = $postdata['evaluation_report'];
            $companyData['review_record'] = $postdata['review_record'];
            $companyData['is_plan'] = $postdata['is_plan'];
            $companyData['record_id'] = $postdata['record_id'];
            $companyData['record_img'] = $postdata['record_img'];
            $companyData['goods_name'] = $postdata['goods_name'];
            $companyData['specification_model'] = $postdata['specification_model'];
            $companyData['total'] = $postdata['total'];
            $companyData['unit'] = $postdata['unit'];
            $companyData['person_charge'] = $postdata['person_charge'];
            $companyData['safety_precautions'] = $postdata['safety_precautions'];
            $companyData['safe_record'] = $postdata['safe_record'];
            $companyData['record_prove_img'] = $postdata['record_prove_img'];
            $companyData['third_party'] = $postdata['third_party'];
            $companyData['organization_name'] = $postdata['organization_name'];
            $companyData['linkman'] = $postdata['linkman'];
            $companyData['phone'] = $postdata['phone'];
            $companyData['descript'] = $postdata['descript'];
            $companyData['j_push_id'] = $postdata['j_push_id'];

            $companyData['create_time'] = time();

            $userData['username'] = $companyData['person_liable'];
            //$userData['phone'] = $companyData['account'];
            //$userData['password'] = $companyData['password'];
            $userData['level_id'] = 5;
            //$userData['company_id'] = $resultCompany;

            $returns['company'] = $companyData;
            $returns['user'] = $userData;
            
            $userM = M('User');
            $findUser = $userM->where('user_id='.$whereUser['user_id'])->find();
            if(!$findUser){
                $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
            }
            

            $companyM = M('Company');
            if(!empty($company_id)){
                //执法者修改
                $resultCompany = $companyM->where('id='.$company_id)->save($companyData);
            }else{
                $resultUser = $userM->where($whereUser)->save($userData);
                $resultCompany = $companyM->where('id='.$findUser['company_id'])->save($companyData);
            }
            

            $this->ajaxReturn(['status' => 1, 'msg' => $returns]);
            exit;
        } else {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }
    /**
     * 企业修改
     */
    public function update_info()
    {
        if (IS_PSOT) {
            $postdata = I('post.');
            /*if (empty($j_push_id)) {
                $this->ajaxReturn(['status' => 0, 'msg' => '请上传推送id！']);
            }*/
            $whereUser['user_id'] = $postdata['user_id'];
            $company_id = $postdata['company_id'];
            if($whereUser['user_id']<=0){
                $this->ajaxReturn(['status' => 0, 'msg' => '缺少user_id']);
            }
            if(!empty($company_id)){
                $whereUser['company_id'] = $company_id;
            }
            $companyData['name'] = $postdata['name'];
            $companyData['code'] = $postdata['code'];
            $companyData['capital'] = $postdata['capital'];
            $companyData['address'] = $postdata['address'];
            $companyData['type'] = $postdata['type'];
            $companyData['gps_position'] = $postdata['gps_position'];
            $companyData['person_liable'] = $postdata['person_liable'];
            $companyData['liable_phone'] = $postdata['liable_phone'];
            $companyData['safety_officer'] = $postdata['safety_officer'];
            $companyData['safety_officer_phone'] = $postdata['safety_officer_phone'];
            $companyData['safety_officer_photo'] = $postdata['safety_officer_photo'];
            //$companyData['account'] = $postdata['account'];
           // $companyData['password'] = md5($postdata['password']);
            $companyData['danger_a'] = $postdata['danger_a'];
            $companyData['danger_b'] = $postdata['danger_b'];
            $companyData['danger_c'] = $postdata['danger_c'];
            $companyData['other'] = $postdata['other'];
            $companyData['create_time'] = time();

            $userData['username'] = $companyData['person_liable'];
            //$userData['phone'] = $companyData['account'];
            //$userData['password'] = $companyData['password'];
            $userData['level_id'] = 5;
            //$userData['company_id'] = $resultCompany;

            $returns['company'] = $companyData;
            $returns['user'] = $userData;
            
            $userM = M('User');
            $findUser = $userM->where('user_id='.$whereUser['user_id'])->find();
            if(!$findUser){
                $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
            }
            

            $companyM = M('Company');
            if(!empty($company_id)){
                //执法者修改
                $resultCompany = $companyM->where('id='.$company_id)->save($companyData);
            }else{
                $resultUser = $userM->where($whereUser)->save($userData);
                $resultCompany = $companyM->where('id='.$findUser['company_id'])->save($companyData);
            }
            

            $this->ajaxReturn(['status' => 1, 'msg' => $returns]);
            exit;
        } else {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }
    
    /**
     * 修改密码
     */
    public function post_change_password()
    {
        $user_id = I('post.user_id');
        $password = I('post.password');
        $new_password = I('post.new_password');
        if (empty($user_id)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
        if (empty($new_password)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '上传新密码！']);
        }
        $user_password = M('User')->where(['user_id' => $user_id])->getField('password');
        if ($user_password != md5($password)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '密码错误！']);
        }
        M('User')->where(['user_id' => $user_id])->setField('password', md5($new_password));
        $this->ajaxReturn(['status' => 1, 'msg' => '修改成功！']);
    }
	
	public function post_worning_send()
	{
		$user_id = I('post.user_id');
		$username = M('User')->where(['user_id'=>$user_id])->getField('username');
		$danger_name = I('post.danger_name');
		$parent1 = M('User')->where(['user_id'=>$user_id])->getField('parent_id');
		if ($parent1 != 0) {
			$user1 = M('User')->where(['user_id'=>$parent1])->find();
			send_sms($user1['phone'], '【' . $danger_name .'】风险点存在重要安全隐患，上报人【' . $username . '】，请及时处理。');
			if ($user1['parent_id'] != 0) {
				$user2 = M('User')->where(['user_id'=>$user1['parent_id']])->find();
				send_sms($user2['phone'], '【' . $danger_name .'】风险点存在重要安全隐患，上报人【' . $username . '】，请及时处理。');
				if ($user2['parent_id'] != 0) {
					$user3 = M('User')->where(['user_id'=>$user2['parent_id']])->find();
					send_sms($user3['phone'], '【' . $danger_name .'】风险点存在重要安全隐患，上报人【' . $username . '】，请及时处理。');
					if ($user3['parent_id'] != 0) {
						$user4 = M('User')->where(['user_id'=>$user3['parent_id']])->find();
						send_sms($user4['phone'], '【' . $danger_name .'】风险点存在重要安全隐患，上报人【' . $username . '】，请及时处理。');
					}
				}
			}
		}
		$this->ajaxReturn(['status' => 1, 'msg' => 'success']);
	}
    /**
     * 发送短信
     * @param $phone string 手机号
     * @param $msg string 内容
     * @return string
     */
    public function http_send_post($mobile, $msg)
    {
        $url = 'http://115.28.96.167:8081/api/sms/send';   //发送接口
        $post_data['userid']       = '351481';    //用户编号
        $post_data['apikey']       =  'ef9ad3d6bdfe4732812f191ca9f107e0';        //apikey  
        $t = explode(" ", microtime());
        $ts = round(round($t[1].substr($t[0],2,3)));
        $post_data['ts']      = $ts ;   //时间戳，精确到毫秒
        $post_data['sign']  = md5($post_data['userid'].$post_data['ts'].$post_data['apikey']);      //签名
        $post_data['mobile']       = $mobile;                //手机号
        $post_data['msgcontent']      = $msg;                     //发送内容

        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);
        $result = $this->request_post($url, $post_data);
    }

    function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }
    
}
